/*
The MIT License (MIT)

Copyright (c) 2022 badlyby

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "dffifo.h"

/*
 * 函数名：DFFIFO_Init
 * 功能：初始化
 * 参数：
 * *fifo：结构体指针
 * *bufA：缓存区A用于存放数据长度
 * sizeA：缓存区A的大小
 * *bufB：缓存区B用于存放数据
 * sizeB：缓存区B的大小
 * 返回值：无
*/
void DFFIFO_Init(DFFIFO *fifo, uint16_t *bufA, uint16_t sizeA, uint8_t *bufB, uint16_t sizeB)
{
    fifo->bufA = bufA;
    fifo->sizeA = sizeA;
    fifo->bufB = bufB;
    fifo->sizeB = sizeB;
#ifdef DFFIFO_PRELOAD
    fifo->preSize = 0;
    fifo->overflow = false;
#endif
    fifo->inited = true;
}

/*
 * 函数名：DFFIFO_GetSizeA
 * 功能：获取缓存区A使用情况
 * 参数：
 * *fifo：结构体指针
 * 返回值：缓存区A占用大小
*/
uint16_t DFFIFO_GetSizeA(DFFIFO *fifo)
{
    if(fifo->inited == false)
        return 0;
    if(fifo-> inIndexA >= fifo-> outIndexA)
    {
        return (fifo->inIndexA - fifo->outIndexA);
    }
    else
    {
        return (fifo->sizeA - fifo->outIndexA + fifo->inIndexA);
    }
}

/*
 * 函数名：DFFIFO_GetSizeB
 * 功能：获取缓存区B使用情况
 * 参数：
 * *fifo：结构体指针
 * 返回值：缓存区B占用大小
*/
uint16_t DFFIFO_GetSizeB(DFFIFO *fifo)
{
    if(fifo->inited == false)
        return 0;
    if(fifo-> inIndexB >= fifo-> outIndexB)
    {
        return (fifo->inIndexB - fifo->outIndexB);
    }
    else
    {
        return (fifo->sizeB - fifo->outIndexB + fifo->inIndexB);
    }
}

#ifdef DFFIFO_PRELOAD
/*
 * 函数名：DFFIFO_PreByte
 * 功能：向预缓存区存入一个字节
 * 参数：
 * *fifo：结构体指针
 * bits：要存入的数据
 * 返回值：存入成功返回true否则返回false
*/
bool DFFIFO_PreByte(DFFIFO *fifo, uint8_t bits)
{
    if(fifo->inited == false)
        return false;
    if(DFFIFO_GetSizeA(fifo) < (fifo->sizeA - 1))
    {
        if((DFFIFO_GetSizeB(fifo) + 1) < fifo->sizeB)
        {
            fifo->bufB[fifo->inIndexB] = bits;
            fifo->inIndexB++;
            fifo->preSize++;
            if(fifo->inIndexB >= fifo->sizeB)
                fifo->inIndexB = 0;
            return true;
        }
        else
            fifo->overflow = true;
    }
    return false;
}
/*
 * 函数名：DFFIFO_PreLoad
 * 功能：向预缓存区存入多个字节
 * 参数：
 * *fifo：结构体指针
 * buf：要存入的数据
 * sz：要存入的数据大小
 * 返回值：存入成功返回true否则返回false
*/
bool DFFIFO_PreLoad(DFFIFO *fifo, uint8_t *buf, uint16_t sz)
{
    uint16_t i;
    if(fifo->inited == false)
        return false;
    if(DFFIFO_GetSizeA(fifo) < (fifo->sizeA - 1))
    {
        if((DFFIFO_GetSizeB(fifo)+sz) < fifo->sizeB)
        {
            for(i=0;i<sz;i++)
            {
                fifo->bufB[fifo->inIndexB] = buf[i];
                fifo->inIndexB++;
                fifo->preSize++;
                if(fifo->inIndexB >= fifo->sizeB)
                    fifo->inIndexB = 0;
            }
            return true;
        }
    }
    return false;
}
/*
 * 函数名：DFFIFO_Complete
 * 功能：预缓存完成，记录写入长度
 * 参数：
 * *fifo：结构体指针
 * 返回值：成功返回true否则返回false
*/
bool DFFIFO_Complete(DFFIFO *fifo)
{
    if(fifo->inited == false)
        return false;
    if(fifo->overflow)
    {
        if(fifo->inIndexB > fifo->preSize)
            fifo->inIndexB -= fifo->preSize;
        else
            fifo->inIndexB = fifo->sizeB - fifo->preSize + fifo->inIndexB;
        fifo->overflow=false;
        fifo->preSize = 0;
        return false;
    }
    if((DFFIFO_GetSizeA(fifo) < (fifo->sizeA - 1)))
    {
        fifo->bufA[fifo->inIndexA] = fifo->preSize;
        fifo->preSize = 0;
        fifo->inIndexA++;
        if(fifo->inIndexA >= fifo->sizeA)
            fifo->inIndexA = 0;
        return true;
    }
    return false;
}
#else
/*
 * 函数名：DFFIFO_Push
 * 功能：存入多个字节数据
 * 参数：
 * *fifo：结构体指针
 * buf：要存入的数据
 * sz：要存入的数据大小
 * 返回值：存入成功返回true否则返回false
*/
bool DFFIFO_Push(DFFIFO *fifo, uint8_t *buf, uint16_t sz)
{
    uint16_t i;
    if(fifo->inited == false)
        return false;
    if((DFFIFO_GetSizeA(fifo) < (fifo->sizeA - 1)) && ((DFFIFO_GetSizeB(fifo) + sz) < fifo->sizeB))
    {
        for(i=0;i<sz;i++)
        {
            fifo->bufB[fifo->inIndexB] = buf[i];
            fifo->inIndexB++;
            if(fifo->inIndexB >= fifo->sizeB)
                fifo->inIndexB = 0;
        }
        fifo->bufA[fifo->inIndexA] = sz;
        fifo->inIndexA++;
        if(fifo->inIndexA >= fifo->sizeA)
            fifo->inIndexA = 0;
        return true;
    }
    return false;
}
#endif //DFFIFO_PRELOAD
/*
 * 函数名：DFFIFO_Pop
 * 功能：取出数据
 * 参数：
 * *fifo：结构体指针
 * buf：存放取出数据的缓存区
 * sz：存放取出数据的缓存区大小
 * 返回值：取出数据的长度
*/
uint16_t DFFIFO_Pop(DFFIFO *fifo, uint8_t *buf, uint16_t sz)
{
    uint16_t i,count,toread;
    if(fifo->inited == false)
        return 0;
    if(DFFIFO_GetSizeA(fifo)>0)
    {
        count = fifo->bufA[fifo->outIndexA];
#ifdef DFFIFO_PRELOAD
        if((DFFIFO_GetSizeB(fifo) - fifo->preSize) < count)
            return 0;
#else
        if(DFFIFO_GetSizeB(fifo) < count)
            return 0;
#endif
        fifo->outIndexA++;
        if(fifo->outIndexA >= fifo->sizeA)
            fifo->outIndexA = 0;
        toread = count;
        if(toread > sz)
            toread = sz;
        for(i=0;i<toread;i++)
        {
            buf[i] = fifo->bufB[fifo->outIndexB];
            fifo->outIndexB++;
            if(fifo->outIndexB >= fifo->sizeB)
                fifo->outIndexB = 0;
        }
        if(toread < count)
        {
            fifo->outIndexB += (count-toread);
            fifo->outIndexB %= fifo->sizeB;
        }
        return toread;
    }
    return 0;
}
